<%@ page import="java.sql.*" %>
<%--
  Created by IntelliJ IDEA.
  User: BIGPIG
  Date: 2024/3/26
  Time: 11:36
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <%
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String sql = "select * from dq_login where username = '" + username + "'and password = '" + password +"'";
        // 注册驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        // 获取连接
        String url = "jdbc:mysql://127.0.0.1:3306/demo";
        String dq_username = "root";
        String dq_password = "123456";
        Connection conn;
        try {
            conn = DriverManager.getConnection(url,dq_username,dq_password);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        Statement stmt;
        try {
            stmt = conn.createStatement();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        ResultSet rs;
        try {
            rs = stmt.executeQuery(sql);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        boolean isValid = false;
        if(rs.next()){
            isValid = true;
        }
        if(isValid){
            try {
                out.println("id :" + rs.getInt("id") + "<br>");
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            try {
                out.println("账号 :" + rs.getString("username") + "<br>");
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            try {
                out.println("密码 :" + rs.getString("password") + "<br>");
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            response.setHeader("refresh","2;URL = " + "index.jsp");
            out.println("用户密码配对成功.........." + "<br>");
            out.println("正在跳转.........." + "<br>");
        }else{
            response.setHeader("refresh","2;URL="+"login.jsp");
            out.println("用户密码配对不正确");
        }
        try {
            rs.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            stmt.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            conn.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    %>
</body>
</html>
